﻿namespace MXERP.Blazor.Components
{
    public class HeaderContext<T>
    {
        private readonly Table<T> _table;

        /// <summary>
        /// The behaviors which are allowed for this header.
        /// </summary>
        public HeaderActions Actions { get; }

        /// <summary>
        /// Indicates whether all items are currently selected.
        /// </summary>
        public bool? IsAllSelected
        {
            get
            {
                if (_table.Selection is not null && _table.RenderedRows.Count > 0)
                {
                    if (_table.Selection.Count == _table.RenderedRows.Count)
                    {
                        return true;
                    }

                    if (_table.Selection.Count == 0)
                    {
                        return false;
                    }

                    return null;
                }

                return false;
            }
        }

        /// <summary>
        /// Creates a new instance.
        /// </summary>
        /// <param name="dataGrid">The <see cref="MudDataGrid{T}"/> which contains the header.</param>
        public HeaderContext(Table<T> table)
        {
            _table = table;
            Actions = new HeaderActions
            {
                SetSelectAll = x => _table.SetSelectAll(x ?? false),
            };

        }

        /// <summary>
        /// Represents the behaviors allowed for a <see cref="MudDataGrid{T}"/> header.
        /// </summary>
        public class HeaderActions
        {
            /// <summary>
            /// The function which selects all items.
            /// </summary>
            public required Func<bool?,Task> SetSelectAll { get; init; }
        }
    }
}